package com.caregiver.watch.business.care.service.impl;

import com.caregiver.watch.business.care.dao.UserAlertRecordDao;
import com.caregiver.watch.business.care.model.bo.AlertCountBo;
import com.caregiver.watch.business.care.model.po.UserAlertRecord;
import com.caregiver.watch.business.care.service.UserAlertRecordService;
import com.github.pagehelper.Page;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.caregiver.watch.common.constant.NumberConstants.ZERO;

/**
 * @Description: 用户告警记录服务
 * @Author 疆戟
 * @Date 2025/11/2 15:08
 * @Version 1.0
 */
@AllArgsConstructor
@Service
@Slf4j
public class UserAlertRecordServiceImpl implements UserAlertRecordService {

    private UserAlertRecordDao userAlertRecordDao;

    @Override
    public boolean insert(UserAlertRecord userAlertRecord) {
        if (Objects.isNull(userAlertRecord)) {
            return false;
        }
        return userAlertRecordDao.insert(userAlertRecord) > ZERO;
    }

    @Override
    public Page<UserAlertRecord> queryWarningHistory(Long userId,
                                                     Long recipientId,
                                                     LocalDateTime startTime,
                                                     LocalDateTime endTime) {
        if (Objects.isNull(userId)) {
            return new Page<>();
        }
        if (Objects.isNull(startTime) || Objects.isNull(endTime)) {
            return new Page<>();
        }
        // 开始时间大于结束时间也返回空
        if (startTime.isAfter(endTime)) {
            return new Page<>();
        }
        return userAlertRecordDao.selectByUserId(userId, recipientId, startTime, endTime);
    }

    @Override
    public List<AlertCountBo> countAlerts(Long userId, LocalDateTime startTime, LocalDateTime endTime) {
        if (Objects.isNull(userId)) {
            return new ArrayList<>();
        }
        if (Objects.isNull(startTime) || Objects.isNull(endTime)) {
            return new ArrayList<>();
        }
        // 开始时间大于结束时间也返回空
        if (startTime.isAfter(endTime)) {
            return new ArrayList<>();
        }
        return userAlertRecordDao.countAlerts(userId, startTime, endTime);
    }
}
